<template>
  <view class="my-cart">
    <view class="shop-title-padding">
      <view class="shop-title">
        <view class="scroll" @change="test">
          <view
            class="sort-item"
            v-for="(item, index) in sortItem"
            :key="index"
            @click="sortClick(item, index)"
            v-bind:class="{ redBorder: isShow == index }"
          >
            <view>{{ item }}</view>
          </view>
        </view>
      </view>
    </view>
    <div class="goods-wrapper">
      <div v-if="isShow == '0'">
        <div
          class="sort-item-content"
          v-for="(orderData, index) in showData"
          :key="index"
        >
          <div class="item-header">{{ orderData.time }}</div>
          <div
            class="content-item"
            v-for="(item, index) in orderData.order_data"
            :key="index"
          >
            <div class="img-wrapper">
              <img class="img" :src="item.img" />
            </div>
            <div class="content-wrapper">
              <div class="title">{{ item.title }}</div>
              <div class="cart-bottom">
                <div class="cart-money">
                  <div class="money">
                    <text class="iconfont icon-jiage"></text>{{ item.price }}
                  </div>
                </div>
                <div class="cart">
                  <div>
                    <div class="btn1" @click="reduce(item, index)">数量:</div>
                  </div>
                  <div class="num">
                    <div>{{ item.num }}</div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="item-foot">
            <div>订单号：{{ orderData.order_id }}</div>
            <div>合计支付：{{ orderData.money }}</div>
          </div>
        </div>
      </div>

      <div v-else-if="isShow == '1'">
        <div
          class="sort-item-content"
          v-for="(orderData, index) in showData"
          :key="index"
        >
          <div class="item-header">请在{{ orderData.t_date }}16:00前提货</div>
          <div class="item-header">提货地点：{{ orderData.address }}</div>
          <div
            class="content-item"
            v-for="(item, index) in orderData.order_data"
            :key="index"
          >
            <div class="img-wrapper">
              <img class="img" :src="item.img" />
            </div>
            <div class="content-wrapper">
              <div class="title">{{ item.title }}</div>
              <div class="cart-bottom">
                <div class="cart-money">
                  <div class="money">
                    <text class="iconfont icon-jiage"></text>{{ item.price }}
                  </div>
                </div>
                <div class="cart">
                  <div>
                    <div class="btn1" @click="reduce(item, index)">数量:</div>
                  </div>
                  <div class="num">
                    <div>{{ item.num }}</div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="item-foot">
            <div>订单号：{{ orderData.order_id }}</div>
            <div>合计支付：{{ orderData.money }}</div>
          </div>
          <div class="item-foot">
            <div class="pickup-code">取货码：{{ orderData.pickup_code }}</div>
            <div class="btn">
              <button
                @click="receiveGoods(orderData.id)"
                class="btn-h"
                size="mini"
                type="default"
              >
                确认收货
              </button>
            </div>
          </div>
        </div>
      </div>

      <div v-else-if="isShow == '2'">
        <div
          class="sort-item-content"
          v-for="(orderData, index) in showData"
          :key="index"
        >
          <!-- <div class="item-header">{{ orderData.time }}</div> -->
          <div
            class="content-item"
            v-for="(item, index) in orderData.order_data"
            :key="index"
          >
            <div class="img-wrapper">
              <img class="img" :src="item.img" />
            </div>
            <div class="content-wrapper">
              <div class="title">{{ item.title }}</div>
              <div class="cart-bottom">
                <div class="cart-money">
                  <div class="money">
                    <text class="iconfont icon-jiage"></text>{{ item.price }}
                  </div>
                </div>
                <div class="cart">
                  <div>
                    <div class="btn1" @click="reduce(item, index)">数量:</div>
                  </div>
                  <div class="num">
                    <div>{{ item.num }}</div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="item-foot">
            <div>订单号：{{ orderData.order_id }}</div>
            <div>合计支付：{{ orderData.money }}</div>
          </div>
        </div>
      </div>
      <div v-if="title" class="title-none">
        <div>暂无数据</div>
      </div>
    </div>
  </view>
</template>

<script>
export default {
  data() {
    return {
      show: true,
      title: false,
      isShow: 0,
      sortItem: ["待确认", "待收货", "已完成"],
      openid: "",
      name: "",
      phone: "",
      address: "幸福小区天天惠超市",
      showData: [
        {
          id: 1,
          img: "http://localhost:3000/161875181886913578870ab610c5.jpg",
          author: 1,
          title:
            "飘柔洗发水套装500ml*3滋润去屑男士女士通用花香调,飘柔洗发水套装500ml*3滋润去屑(男士女士通用)花香调",
          sort: "经典之作",
          fileName: "classicWorks|sanGuo",
          num: 5,
        },
        {
          id: 2,
          img: "http://localhost:3000/1618751818854908fa0ec08fa513dfd281107336d55fbb3fbd96d.jpg",
          author: 2,
          title:
            "飘柔洗发水套装500ml*3滋润去屑男士女士通用花香调,飘柔洗发水套装500ml*3滋润去屑(男士女士通用)花香调",
          sort: "经典之作",
          fileName: "classicWorks|sanGuo",
          num: 10,
        },
        {
          id: 3,
          img: "https://img12.360buyimg.com/n1/jfs/t1/140716/6/14212/152277/5faccda7Ee80ce57b/97dcbe262dd41fef.jpg",
          author: 3,
          title:
            "飘柔洗发水套装500ml*3滋润去屑男士女士通用花香调,飘柔洗发水套装500ml*3滋润去屑(男士女士通用)花香调",
          sort: "经典之作",
          fileName: "classicWorks|sanGuo",
        },
        {
          id: 4,
          img: "https://img12.360buyimg.com/n1/jfs/t24718/142/1801401224/452184/39730568/5bbc0176N26b6eba6.jpg",
          author: 4,
          title:
            "飘柔洗发水套装500ml*3滋润去屑男士女士通用花香调,飘柔洗发水套装500ml*3滋润去屑(男士女士通用)花香调",
          sort: "经典之作",
          fileName: "classicWorks|sanGuo",
        },
      ],
      allPrice: 0,
    };
  },
  onShow() {
    let that = this;
    uni.getStorage({
      key: "openid",
      success: function (res) {
        // console.log(res);
        that.openid = res.data;
        that.requestData();
      },
    });
  },
  //自定义返回
  // onUnload() {
  //   // uni.redirectTo({ url: "/pages/home/service/GroupBuying/ShopHome" });

  //   console.log("fanhui ");
  //   return true;
  // },
  methods: {
    requestData() {
      uni.showLoading({
        title: "加载中",
        mask: true,
      });
      this.showData = [];
      this.$MyRequest({
        url: "/cart_order_my",
        method: "POST",
        data: {
          openid: this.openid,
        },
      }).then((res) => {
        console.log(res.data);
        uni.hideLoading();
        this.showData = res.data.data.map((s) => {
          s.order_data = JSON.parse(s.order_data);
          return s;
        });

        this.showData = this.showData.sort((a, b) => {
          return a.id < b.id ? 1 : -1;
        });
        if (this.isShow == "0") {
          this.showData = this.showData.filter((s) => s.status < 0);
        }
        if (this.isShow == "1") {
          this.showData = this.showData.filter((s) => s.status == 1);
        }
        if (this.isShow == "2") {
          this.showData = this.showData.filter((s) => s.status == 2);
        }
        if (this.showData.length <= 0) {
          this.title = true;
        } else {
          this.title = false;
        }
        console.log(this.showData);
        // this.questionFrom = JSON.parse(optionData);
        // console.log(optionData);
      });
    },
    sortClick(item, index) {
      this.isShow = index;
      console.log(this.isShow);
      this.requestData();
      // switch (item) {
      //   case this.sortItem[0]:
      //     console.log(item);
      //     break;
      //   case this.sortItem[1]:
      //     console.log(item);
      //     break;
      //   case this.sortItem[2]:
      //     console.log(item);
      //     break;

      //   default:
      //     break;
      // }
    },
    //确认收货
    receiveGoods(id) {
      let that = this;
      uni.showModal({
        title: "请确认是否收货？",
        content: "请确认商品收到并检查无误后收货！",
        cancelText: "取消",
        confirmText: "确认收货",
        cancelColor: "#999",
        confirmColor: "#E92828",
        success(res) {
          if (res.confirm) {
            that
              .$MyRequest({
                url: "/cart_order_recive",
                method: "POST",
                data: {
                  id: id,
                },
              })
              .then((res) => {
                console.log(res);
                uni.showToast({
                  title: "收货成功",
                  icon: "success",
                  duration: 1500,
                });

                that.isShow = 2;
                that.requestData();
                // uni.navigateTo({
                //   url: "/pages/home/service/GroupBuying/PaySucess",
                // });
              });
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
    },
  },
};
</script>

<style lang="scss">
@import "@/static/styles/mixin.scss";

page {
  height: 100%;
  background-color: $uni-color-indexBg;
}

.my-cart {
  background-color: $uni-color-indexBg;
  width: 100%;
  height: 100%;
  // height: 100%;
}
.shop-title-padding {
  // padding-right: 30rpx;
  .shop-title {
    // margin-top: 15px;
    // padding-right: 30rpx;
    background-color: #fff;
    border-bottom: 1rpx solid rgb(230, 227, 227);
    .scroll {
      font-size: 36rpx;
      // padding-right: 30rpx;
      height: 70rpx;
      white-space: nowrap;
      overflow-x: scroll;
      // overflow-x: hidden;

      display: flex;
      box-sizing: border-box;
      // border-bottom: 1rpx solid rgb(209, 209, 209);

      .sort-item {
        width: 300rpx;
        padding: 0 15rpx;
        padding-bottom: 5rpx;
        box-sizing: border-box;
        // border-bottom: 5rpx solid rgb(216, 16, 16);
        @include center;
      }
      .redBorder {
        width: 300rpx;
        padding: 0 15rpx;
        box-sizing: border-box;
        color: rgb(231, 59, 59);
        border-bottom: 5rpx solid rgb(245, 2, 2);
        @include center;
      }
    }
  }
}
.goods-wrapper {
  margin-top: 20rpx;
  .item-header {
    color: #666;
    margin-bottom: 10rpx;
  }
  .sort-item-content {
    padding: 0 15rpx;
    padding-top: 15rpx;
    // padding-bottom: 10rpx;
    background-color: #fff;
    margin-bottom: 20rpx;
    width: 100%;

    box-sizing: border-box;

    .content-item {
      display: flex;
      margin-bottom: 20rpx;
      // border-bottom: 1rpx solid rgba(223, 222, 222, 0.746);
      // padding-bottom: 10px;

      .img-wrapper {
        // padding: 20rpx 20rpx 20rpx 0;
        box-sizing: border-box;
        flex: 0 0 28%;
        // width: 250rpx;
        // height: 270rpx;
        .img {
          width: 100%;
          height: 180rpx;
        }
      }
      .content-wrapper {
        width: 100%;
        height: 180rpx;
        padding: 5rpx 0;
        padding-left: 18rpx;
        box-sizing: border-box;
        position: relative;

        padding-bottom: 10px;
        // padding-right: 15rpx;
        // right: 15rpx;
        .title {
          font-size: 24rpx;
          letter-spacing: 2rpx;
          color: black;
        }
        .author {
          margin-top: 20rpx;
        }
        .cart-bottom {
          position: absolute;
          bottom: 5px;
          display: flex;
          .cart-money {
            .money {
              color: #000;
              font-size: 30rpx;
              // font-weight: bold;
              // @include center;
            }
          }
          .cart {
            // padding-left: 120rpx;
            // box-sizing: border-box;
            position: absolute;
            left: 290rpx;
            bottom: 0;
            font-size: 30rpx;
            box-sizing: border-box;
            display: flex;
            color: rgb(163, 163, 163);
            .num {
              width: 68rpx;
            }
            .btn1 {
              font-size: 30rpx;

              width: 90rpx;
            }
          }
        }
      }
    }
  }
  .item-foot {
    display: flex;
    height: 80rpx;
    justify-content: space-between;
    align-items: center;
    color: #666;
    font-size: 30rpx;
    border-top: 1px solid rgba(223, 222, 222, 0.746);
    .pickup-code {
      color: rgb(0, 0, 0);
      font-weight: bold;
    }
    .btn {
      @include center;
      .btn-h {
        height: 60rpx;
        @include center;
        color: $uni-color-cart;
      }
    }
  }
  .title-none {
    @include center;
  }
}
</style>
